import cryptoJs from 'crypto-js'

const AES_KEY = '8NONwyJtHesysWpM'

export default {
  generate(uname, pword) {
    uname = uname.replace(/\\%/g, '')
    pword = pword.replace(/\\%/g, '')
    uname = uname.replace(/\\</g, '')
    pword = pword.replace(/\\</g, '')
    uname = uname.replace(/\\>/g, '')
    pword = pword.replace(/\\>/g, '')

    return this.Encrypt(uname + ',' + pword)
  },
  Encrypt(content) {
    const key = cryptoJs.enc.Utf8.parse(AES_KEY)
    const srcs = cryptoJs.enc.Utf8.parse(content)
    const encrypted = cryptoJs.AES.encrypt(srcs, key, {
      mode: cryptoJs.mode.ECB,
      padding: cryptoJs.pad.Pkcs7
    })
    return encrypted.toString()
  },
  Decrypt(content) {
    const key = cryptoJs.enc.Utf8.parse(AES_KEY)
    const decrypt = cryptoJs.AES.decrypt(content, key, {
      mode: cryptoJs.mode.ECB,
      padding: cryptoJs.pad.Pkcs7
    })
    return cryptoJs.enc.Utf8.stringify(decrypt).toString()
  }
}
